package com.lucy.nepu.aspect;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Validator;
import com.lucy.common.anno.WebLog;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.exception.BusinessException;
import com.lucy.common.utils.JsonUtil;
import com.lucy.nepu.anno.WorkloadLimit;
import com.lucy.nepu.data.workload.entity.Workload;
import com.lucy.nepu.data.workload.service.WorkloadService;
import com.lucy.nepu.loader.PeriodLoader;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.hibernate.jdbc.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

/**
 * @author kindear
 * 工作量记录限制切面
 */
@Slf4j
@Aspect
@Component
public class WorkloadLimitAspect {
    @Autowired
    WorkloadService workloadService;

    @Autowired(required = false)
    PeriodLoader periodLoader;

    /**
     * 以自定义注解作为切入
     */
    @Pointcut("@annotation(com.lucy.nepu.anno.WorkloadLimit)")
    public void WorkloadLimit(){}

    /**
     * 切面
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("WorkloadLimit()")
    public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取注解
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        // 获取自定义注解信息
        WorkloadLimit workloadLimit = method.getAnnotation(WorkloadLimit.class);
        // 工作量条数限制
        int workloadLimitNum = workloadLimit.num();
        // 构建查询条件
        Workload workload = new Workload();
        // 获取用户ID
        Long userId = StpUtil.getLoginIdAsLong();
        workload.setUserId(userId);
        // 获取周期
        workload.setPeriodId(periodLoader.getPeriodId());
        // 获取类型
        String type = workloadLimit.type();
        if (Validator.isNotEmpty(type)){
            workload.setWorkloadType(type);
        }
        Example<Workload> example = Example.of(workload);
        List<Workload> list = workloadService.getExample(example);
        // 超出工作量记录，无法继续申报
        if(workloadLimitNum <= list.size()){
            throw new BusinessException(HttpStatus.FORBIDDEN,"工作量记录不可超出"+workloadLimitNum);
        }
        // 计算工作量总数
        double workloadTotalLimit = workloadLimit.total();
        double totalWorkload = 0.0;
        for (Workload ele:list){
            totalWorkload = totalWorkload + ele.getWorkload();
        }
        if (workloadTotalLimit <= totalWorkload){
            throw new BusinessException(HttpStatus.FORBIDDEN,"工作量记录总和不可超出"+workloadTotalLimit);
        }

        // 执行方法
        return joinPoint.proceed();
    }


}
